home *** CD-ROM | disk | FTP | other *** search
/ Stone Design / Stone Design.iso / Stone_Friends / Wave / WavesWorld / Source / IBPalettes / WW3DKit / WW3DAttributeState.m < prev    next >
Encoding:
Text File  |  1995-03-22  |  3.0 KB  |  133 lines

  1.  
  2. #import "WW3DAttributeState.h"
  3.  
  4. #import "usefulWW3DFunctions.h"
  5.  
  6. @implementation WW3DAttributeState
  7.  
  8. + initialize { return [WW3DAttributeState setVersion:1], self; }
  9.  
  10. - init
  11. {
  12.   [super init];
  13.   N3D_CopyMatrix(N3DIdentityMatrix, ctm);
  14.   boundingBox[0] = 0.0;
  15.   boundingBox[1] = 0.0;
  16.   boundingBox[2] = 0.0;
  17.   boundingBox[3] = 0.0;
  18.   boundingBox[4] = 0.0;
  19.   boundingBox[5] = 0.0;
  20.   hasBoundingBox = NO;
  21.  
  22.   return self;
  23. }
  24.  
  25. - (BOOL)hasBoundingBox { return hasBoundingBox; }
  26. - setHasBoundingBox:(BOOL)flag { hasBoundingBox = flag;  return self; }
  27.  
  28. - growBoundingBox:(RtBound *)newBoundingBox
  29. {
  30.   if (hasBoundingBox)
  31.   {  // we need to "grow" our boundingBox w re: to this new one
  32.      // the idea here is that our boundingBox is current to our ctm,
  33.      // but this incoming bound needs to be transformed by the ctm.  
  34.      // Once it's been transformed into the same space, we recompare 
  35.      // bounding boxes, and update ours based on the mins and maxes
  36.      // of those two bounds
  37.      RtBound  tmpBoundingBox;
  38.  
  39.  
  40.      WW3DDetermineBoundGivenBoundAndCTM(&tmpBoundingBox, newBoundingBox,  ctm);
  41.      //// X
  42.      if (tmpBoundingBox[0] < boundingBox[0])
  43.      {  boundingBox[0] = tmpBoundingBox[0];
  44.      }
  45.      if (tmpBoundingBox[1] > boundingBox[1])
  46.      {  boundingBox[1] = tmpBoundingBox[1];
  47.      }
  48.      //// Y
  49.      if (tmpBoundingBox[2] < boundingBox[2])
  50.      {  boundingBox[2] = tmpBoundingBox[2];
  51.      }
  52.      if (tmpBoundingBox[3] > boundingBox[3])
  53.      {  boundingBox[3] = tmpBoundingBox[3];
  54.      }
  55.      //// Z
  56.      if (tmpBoundingBox[4] < boundingBox[4])
  57.      {  boundingBox[4] = tmpBoundingBox[4];
  58.      }
  59.      if (tmpBoundingBox[5] > boundingBox[5])
  60.      {  boundingBox[5] = tmpBoundingBox[5];
  61.      }
  62.   }
  63.   else // first time; set 
  64.   {  N3D_CopyBound(*newBoundingBox, boundingBox);
  65.      // now transform these points by the ctm
  66.      WW3DDetermineBoundGivenBoundAndCTM(&boundingBox, &boundingBox,  ctm);
  67.      hasBoundingBox = YES;
  68.   }
  69.  
  70.   return self;
  71. }
  72.  
  73. - (RtBound *)boundingBox {  return &boundingBox; }
  74.  
  75. - setTransformMatrix:(RtMatrix)newMatrix
  76. {
  77.   N3D_CopyMatrix(newMatrix, ctm);
  78.   return self;
  79. }
  80.  
  81. - getTransformMatrix:(RtMatrix)matrix
  82. {
  83.   N3D_CopyMatrix(ctm, matrix);
  84.   return self;
  85. }
  86.  
  87. - translate:(RtFloat)dx :(RtFloat)dy :(RtFloat)dz
  88. {
  89.   RtMatrix  tmp;
  90.  
  91.  
  92.   N3D_CopyMatrix(N3DIdentityMatrix, tmp);
  93.   tmp[3][0] = dx;
  94.   tmp[3][1] = dy;
  95.   tmp[3][2] = dz;
  96.  
  97.   N3DMultiplyMatrix(ctm, tmp, ctm);
  98.  
  99.   return self;
  100. }
  101.  
  102.  
  103. - read:(NXTypedStream*)stream 
  104. {
  105.     int version;
  106.     [super read:stream];
  107.  
  108.     version = NXTypedStreamClassVersion(stream,"WW3DAttributeState");
  109.     if (version == 0) NXReadTypes(stream, "i", &version), version=1;
  110.     if (version == 1)
  111.     {  NXReadArray(stream, "f", 4, ctm[0]);
  112.        NXReadArray(stream, "f", 4, ctm[1]);
  113.        NXReadArray(stream, "f", 4, ctm[2]);
  114.        NXReadArray(stream, "f", 4, ctm[3]);
  115.     } 
  116.  
  117.     return self;
  118. }
  119.  
  120. - write:(NXTypedStream *)stream 
  121. {
  122.     [super write:stream];
  123.  
  124.     NXWriteArray(stream, "f", 4, ctm[0]);
  125.     NXWriteArray(stream, "f", 4, ctm[1]);
  126.     NXWriteArray(stream, "f", 4, ctm[2]);
  127.     NXWriteArray(stream, "f", 4, ctm[3]);
  128.  
  129.     return self;
  130. }
  131.  
  132. @end
  133.